home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / Z4EXTRCT.CPP < prev    next >
C/C++ Source or Header  |  1993-08-23  |  7KB  |  243 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    z4extrct.cpp
  5. //   Title:    9-Digit ZIP Code Directory -- on CD-ROM
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //    This module contains the program entry point for
  24. //
  25. //    The code in this module may be written in C++ or C.
  26. //
  27. //    This module is portable to:
  28. //        DOS 3.X+
  29. //        MS Windows 3.X+
  30. //        OS/2 2.X+
  31. //        OS/2 2.0 PM
  32. //
  33. //    The following compilers are supported:
  34. //        MSC 6.0A
  35. //        MSC/C++ 7.0
  36. //        Borland C++ 3.1 for DOS
  37. //        Borland C++ 1.0 for OS/2 2.X
  38. //
  39. //----------------------------------------------------------------------------
  40. #include <z4.h>
  41.  
  42.  
  43. //----------------------------------------------------------------------------
  44. //    Stack size
  45. //----------------------------------------------------------------------------
  46. #if COMPILER_BORLAND && (OS_DOS || OS_WINDOWS)
  47. unsigned _stklen = 0x4000;
  48. #endif
  49.  
  50.  
  51. //----------------------------------------------------------------------------
  52. //    Globals
  53. //----------------------------------------------------------------------------
  54.  
  55. //
  56. // A useless string which is simply embedded in the executable.
  57. //
  58. PSZ __pszCredits__ = "Written by John M Weeder, 1993";
  59.  
  60.  
  61. //
  62. //    This should contain a program description which will be displayed as
  63. //    part of the program's help text.
  64. //
  65. static PCSZ pcszDescription =
  66.     "This program extracts records from a ZIP+4 data file.";
  67.  
  68. //
  69. //    Program command line options
  70. //
  71. static Z4_Z4 z4;
  72. static CHAR szOutput[MAX_PATH];
  73. static HF hfOutput = -1;
  74. static LONG lStart = 0;
  75. static LONG lEnd = -1;
  76. static LONG lLength = -1;
  77. static LONG lRange = 1;
  78. static LONG lSkip = 0;
  79. static BOOL fAppend = FALSE;
  80. static CHAR szFormat[1024];
  81. static BS_CMDOPT acmdopt[] =
  82.     {
  83.     { "APPEND", (PVOID)&fAppend,         CMDOPT_TRUE,                "Append records to output?"},
  84.     { "BEGIN",     (PVOID)&lStart,         CMDOPT_NUMERIC,            "First record to extract."},
  85.     { "END",     (PVOID)&lEnd,             CMDOPT_NUMERIC,            "Last record to extract."},
  86.     { "LENGTH", (PVOID)&lLength,         CMDOPT_NUMERIC,            "Length of records to extract."},
  87.     { "RANGE",     (PVOID)&lRange,         CMDOPT_NUMERIC,            "Range of records to extract."},
  88.     { "SKIP",     (PVOID)&lSkip,         CMDOPT_NUMERIC,            "Number of records to skip."},
  89.     { "output",    (PVOID)szOutput,         CMDOPT_FILESPECR( 80),  "Output file name."},
  90.     BS_CMDOPT_NULL,
  91.     };
  92.  
  93.  
  94. //----------------------------------------------------------------------------
  95. //   Description:    main() - Program entry point
  96. //    Parameters:    Standard C parameters
  97. //       Returns:    DOS return code.
  98. //----------------------------------------------------------------------------
  99. BOOL FN_E Write()
  100. {
  101.     PCSZ pszRecType = Z4Z4RecordType(z4.rectype, Z4_DATA_FULL);
  102.  
  103.     if (z4.suffix2)
  104.         strcats(z4.szPriName, Z4SuffixFull(z4.suffix2));
  105.  
  106.     sprintf(szFormat,
  107.         "%-30.30s  %-4s %-6s - %6s %*s** %s **\r\n"
  108.         "%-10s - %-10s %-2s %-30s %-4s %-2s\r\n"
  109.         "%s %-4s - %-4s\r\n\r\n",
  110.         z4.szSecName,
  111.         Z4Unit(z4.unit, Z4_DATA_ABBREV),
  112.         z4.szSecLo,
  113.         z4.szSecHi,
  114.         20 - strlen(pszRecType), "",
  115.         pszRecType,
  116.         z4.szPriLo,
  117.         z4.szPriHi,
  118.         Z4Directional(z4.predir, Z4_DATA_ABBREV),
  119.         z4.szPriName,
  120.         Z4SuffixAbbrev(z4.suffix1),
  121.         Z4Directional(z4.postdir, Z4_DATA_ABBREV),
  122.         z4.szZip5,
  123.         z4.szAddonHi,
  124.         z4.szAddonLo);
  125.     return FileWrite(hfOutput, szFormat, strlen(szFormat), -1);
  126. }
  127.  
  128.  
  129. //----------------------------------------------------------------------------
  130. //   Description:    main() - Program entry point
  131. //    Parameters:    Standard C parameters
  132. //       Returns:    DOS return code.
  133. //----------------------------------------------------------------------------
  134. int main(int argc, char **argv)
  135. {
  136. static BS_CFG cfg = CFG_DFT;
  137.     FLAG16 fsOutput = FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
  138.     PCSZ pcszData = EnvGet("DATA");
  139.     SHORT sResult = 99;
  140.     LONG lRecords;
  141.     BYTE bKey[2][MAX_Z4_KEY];
  142.     SIZET cWhich = 0;
  143.     LONG lLast = -1;
  144.  
  145.     //
  146.     //    Initialize base library
  147.     //
  148.     BaseLibraryInitialize(argc, argv, &cfg);
  149.     BaseTitle("$Revision:  93.1  $", __DATE__, __TIME__, "ZIP+4 Record Extract Utility");
  150.     if (!BaseTitleHelp(acmdopt, pcszDescription))
  151.         return 99;
  152.  
  153.     if (szOutput[0])                            // Open output file
  154.         {
  155.        if (!FnameQualify(szOutput, NULL, pcszData, 0))
  156.            goto ERROR_EXIT;
  157.        if (!fAppend)
  158.            fsOutput |= (FL_CREATE|FL_TRUNCATE);
  159.        else
  160.            fsOutput |= (FL_CREATE|FL_OPEN);
  161.        if (!FileOpen(&hfOutput, szOutput, fsOutput, NULL))
  162.            goto ERROR_EXIT;
  163.        Output("Opened output file '%s'\n", szOutput);
  164.         }
  165.                                                     // Open data files
  166.     if (!Z4_INQ::Start(Z4_INQ_CS|Z4_INQ_CX|Z4_INQ_AB|Z4_INQ_ST|Z4_INQ_Z4|Z4_INQ_Z5))
  167.         {
  168.         Error(
  169.             "There is a problem accessing the data files.\n"
  170.             "Please verify that the program is configured correctly.");
  171.         goto ERROR_EXIT;
  172.         }
  173.     if (lStart < 0)
  174.         lStart = 0;
  175.     if (lSkip < 0)
  176.         lSkip = 0;
  177.     lRecords = Z4_INQ::z4_file.Records();
  178.     if (lRecords == 0)
  179.         {
  180.         Error("No records found in input file.\n");
  181.         goto ERROR_EXIT;
  182.         }
  183.     if (lStart >= lRecords)
  184.         {
  185.         Output("No records copied.\n");
  186.         goto ERROR_EXIT;
  187.         }
  188.     if (lLength > 0)
  189.         lEnd = lStart + lLength - 1;
  190.     if (lEnd >= lRecords || lEnd < lStart)
  191.         lEnd = lRecords - 1;
  192.     if (lRange <= 0)
  193.         lRange = 1;
  194.  
  195.     memset(bKey, 0, sizeof(bKey));
  196.  
  197.     while (lStart <= lEnd)
  198.         {
  199.         for (LONG l = 0; l < lRange && lStart <= lEnd; ++l, ++lStart)
  200.             {
  201.             if (lLast < 0 ||
  202.             ((lStart - lLast) % 1000L) == 0)
  203.                 {
  204.               while (KbdReady())
  205.                   if (KbdChar() == ESC)
  206.                       {
  207.                       Output("\r%-40s\rAborted.\n", "");
  208.                       goto ERROR_EXIT;
  209.                       }
  210.                 Output("\r%08ld", lStart);
  211.                 lLast = lStart;
  212.                 }
  213.             if (!Z4_INQ::z4_file.Record(z4, lStart))
  214.                 {
  215.                 Output("\n\nFailed reading record %ld.\n", lStart);
  216.                 goto ERROR_EXIT;
  217.                 }
  218.             cWhich = !cWhich;
  219.             if (szOutput[0])                    // Open output file
  220.                 if (!Write())
  221.                     goto ERROR_EXIT;
  222.  
  223.             Z4Z4Key(bKey[cWhich], z4.szFinance, z4.szPriName, z4.szPriLo);
  224.             if (memcmp(bKey[cWhich], bKey[!cWhich], MAX_Z4_KEY) < 0)
  225.                 {
  226.                 Output("\n\nInvalid sort sequence at record %ld.\n", lStart);
  227.                 goto ERROR_EXIT;
  228.                 }
  229.             }
  230.         lStart += lSkip;                        // Move to next record
  231.         }
  232.     Output("\r%08ld\nSuccess\n", lEnd);
  233.  
  234. ERROR_EXIT:
  235.     Z4_INQ::Terminate();                        // Close data files
  236.     if (hfOutput >= 0)                        // Close output
  237.         FileClose(hfOutput);
  238.     return sResult;
  239. }
  240. //----------------------------------------------------------------------------
  241. //------------------------------- End of File --------------------------------
  242. //----------------------------------------------------------------------------
  243.